package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Student;
import com.itheima.edu.info.manager.service.StudentService;

import java.util.Scanner;

public class BaseStduentController {
    Scanner sc=new Scanner(System.in);
    StudentService studentService=new StudentService();
    public void start() {
        //开启学生管理系统，并展示菜单
        co:
        while(true){
            System.out.println("-------欢迎来到学生管理系统--------");
            System.out.println("1.添加学生");
            System.out.println("2.删除学生");
            System.out.println("3.修改学生");
            System.out.println("4.查看所有学生");
            System.out.println("5.退出");
            System.out.println("请输入你的选择:");
            int choice = sc.nextInt(); //同一个sc对象，先输入数据，下一次输入字符串的时候会自动跳过
            switch (choice){
                case 1 :
                    //System.out.println("添加学生");
                    addStudent();
                    break;
                case 2 :
                   // System.out.println("删除学生");
                    deleteStudent();
                    break;
                case 3 :
                    //System.out.println("修改学生");
                    updateStudent();
                    break;
                case 4 :
                   // System.out.println("查看所有学生");
                    findAllStudent();
                    break;
                case 5 :
                    System.out.println("感谢您的使用，再见！");
                    break co;
                default :
                    System.out.println("您输入有误，请重新选择");
                    break;
            }
        }

    }

    public void updateStudent() {

       // Scanner sc=new Scanner(System.in);
        System.out.println("请输入您要修改的学生学号：");
        while(true){
            String sid = sc.next();
            boolean flag = studentService.isExists(sid);
            if(!flag){
                System.out.println("您输入的学号不存在，请重新输入");
            } else{
                System.out.println("请输入新的学生姓名：");
                String name = sc.next();
                System.out.println("请输入新的学生年龄：");
                String age = sc.next();
                System.out.println("请输入新的学生生日：");
                String birthday = sc.next();
                Student student = new Student();
                student.setId(sid);
                student.setName(name);
                student.setAge(age);
                student.setBirthday(birthday);
                studentService.updataStudent(student,sid);
                System.out.println("修改成功");
                break;
            }
        }
    }
    //删除学生
    public void deleteStudent() {
       // Scanner sc=new Scanner(System.in);
        System.out.println("请输入您要删除的学生学号：");
        while(true){
                String sid = sc.next();
                boolean flag = studentService.isExists(sid);
                if(!flag){
                    System.out.println("您输入的学号不存在，请重新输入");

                } else{
                    studentService.deleteStudent(sid);
                    System.out.println("删除成功！");
                    break;
            }
        }
    }

    public void findAllStudent() {
       Student[] stu= studentService.finAllStudent();
        if(stu==null){
            System.out.println("查无信息！");
            return;
        }
        System.out.println("学号\t\t姓名\t年龄\t生日");
        for (int i = 0; i < stu.length; i++) {
            if(stu[i]!=null){
                System.out.println(stu[i].getId() +"\t"+stu[i].getName()+ "\t" +stu[i].getAge()+ "\t\t" +stu[i].getBirthday());
            }
        }
    }
    //添加学生
    public void addStudent() {
        //StudentService studentService=new StudentService();
        String id;
        while(true){
            System.out.println("请输入学生学号：");
            id = sc.next();
            boolean flag=studentService.isExists(id);
            if(flag){
                System.out.println("学号已被占用，请重新输入");
            }else{
                break;
            }
        }

        System.out.println("请输入学生姓名：");
        String name = sc.next();
        System.out.println("请输入学生年龄：");
        String age = sc.next();
        System.out.println("请输入学生生日：");
        String birthday = sc.next();
        Student stu=new Student();
        stu.setId(id);
        stu.setName(name);
        stu.setAge(age);
        stu.setBirthday(birthday);
        boolean result=studentService.addStudent(stu);
        if(result){
            System.out.println("添加成功！");
        }else{
            System.out.println("添加失败！");
        }
    }
}
